const app = {
  state: {
    theme: uni.getStorageSync('theme') || 'light',
    systemInfo: {},
  },
  mutations: {
    CHANGE_THEME(state) {
      state.theme = uni.getStorageSync('theme', 'light') == 'light' ? 'dark' : 'light'
      uni.setStorageSync('theme', state.theme)
      setTabbarColor(state.theme)
      // #ifdef APP-PLUS
      setNavigationBarColor(state.theme)
      // #endif
    },
    SET_THEME(state) {
      setTabbarColor(state.theme)
      // #ifdef APP-PLUS
      setNavigationBarColor(state.theme)
      // #endif
    },
    SET_SYSTEM_INFO(state) {
      state.systemInfo = uni.getSystemInfoSync()
    },
  },
  actions: {
    changeTheme({ commit }, data) {
      commit('CHANGE_THEME', data)
    },
  },
}

function setTabbarColor(theme) {
  uni.setTabBarStyle({
    // color: theme === 'light' ? 'rgb(128, 128, 128)' : 'rgba(255, 255, 255,0.6)',
    selectedColor: theme == 'light' ? '#000000' : '#ffffff',
    backgroundColor: theme == 'light' ? '#ffffff' : '#000000',
    borderStyle: theme === 'light' ? 'black' : 'white',
  })
}

function setNavigationBarColor(theme) {
  uni.setNavigationBarColor({
    frontColor: theme == 'light' ? '#000000' : '#ffffff',
    backgroundColor: theme == 'light' ? '#ffffff' : '#000000',
  })

  // 安卓底部小白条
  try {
    const systemInfo = uni.getSystemInfoSync()
    if (systemInfo.osName === 'android') {
      const Color = plus.android.importClass('android.graphics.Color')
      plus.android.importClass('android.view.Window')
      const mainActivity = plus.android.runtimeMainActivity()
      const window_android = mainActivity.getWindow()
      window_android.setNavigationBarColor(Color.parseColor(theme == 'light' ? '#ffffff' : '#000000'))
    }
  } catch (error) {
    console.warn(error)
  }
}

export default app
